package edu.suda.LoopCodePlus.controller;

import com.auth0.jwt.interfaces.DecodedJWT;
import edu.suda.LoopCodePlus.common.Result;
import edu.suda.LoopCodePlus.entity.Solution;
import edu.suda.LoopCodePlus.service.SolutionService;
import edu.suda.LoopCodePlus.util.JwtUtil;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/solution")
@Slf4j
public class SolutionController {
    @Autowired
    SolutionService solutionService;
    @Autowired
    JwtUtil jwtUtil;
    @GetMapping("/get")
    public Result get(@RequestParam("problemId") Integer problemId, @RequestParam("page") Integer page) {
        System.out.println("参数验证");
        if(problemId==null||page==null){
            System.out.println("参数验证失败");
            return Result.failure();
        }
        System.out.println("参数验证通过");
        List<Solution> solutionList;
        try {
            solutionList = solutionService.findAllSolution(problemId);
            if(solutionList==null){
                System.out.println("数据库查询失败");
                return Result.failure();
            }else {
                System.out.println("数据库查询成功:"+solutionList);
                if((page-1)*10>=solutionList.size()){
                    System.out.println("参数page越界");
                    return Result.failure(0);
                }
                return Result.success(solutionList.subList((page-1)*10, Math.min(solutionList.size(), page * 10)));
            }
        }catch (Exception e){
            System.out.println("数据库查询失败");
            return Result.failure();
        }
    }

    @PostMapping("/add")
    public Result add(@RequestBody Solution solution,
                         HttpServletRequest request,
                         HttpServletResponse response){
        System.out.println("收到题解新增/更新请求");
        System.out.println("验证token");
        String token = request.getHeader("Authorization");
        Integer userId;
        try {
            DecodedJWT decodedJWT = jwtUtil.verifyToken(token);
            userId = Integer.parseInt(decodedJWT.getClaim("userId").asString());
            System.out.println("token验证成功");
        }catch (Exception e){
            System.out.println("token验证失败");
            response.setStatus(HttpStatus.UNAUTHORIZED.value());
            return null;
        }
        System.out.println("参数验证");
        if(solution.getContent()==null||solution.getProblemId()==null||solution.getSolutionName()==null||solution.getTag()==null||solution.getUserId()==null){
            System.out.println(solution.getContent()+solution.getProblemId()+solution.getSolutionName()+solution.getTag()+solution.getUserId());
            System.out.println("参数验证失败");
            return Result.failure();
        }
        System.out.println("参数验证通过");
        if(solution.getSolutionId()!=null){
            System.out.println("更新题解"+solution);
            try{
                if(solutionService.updateSolution(userId,solution.getSolutionId(),solution)==0){
                    System.out.println("数据库更新成功");
                    return Result.success();
                }else if(solutionService.updateSolution(userId,solution.getSolutionId(),solution)==1){
                    System.out.println("该题解不存在");
                    return Result.failure("题解不存在");
                } else if (solutionService.updateSolution(userId,solution.getSolutionId(),solution)==2) {
                    System.out.println("权限不足");
                    return Result.failure("权限不足");
                }else{
                    System.out.println("数据库操作发生错误");
                    return Result.failure("数据库操作发生错误");
                }
            }catch (Exception e){
                System.out.println("数据库操作发生错误");
                return Result.failure("数据库操作发生错误");
            }

        }
        System.out.println("插入新题解");
        try{
            if(solutionService.insertSolution(solution)){
                System.out.println("数据库插入成功");
                return Result.success();
            }else {
                System.out.println("数据库插入失败");
                return Result.failure();
            }
        }catch (Exception e){
            System.out.println("数据库插入失败");
            return Result.failure();
        }
    }

    @DeleteMapping("/delete")
    public Result delete(@RequestParam("solutionId") Integer solutionId,
                         HttpServletRequest request,
                         HttpServletResponse response){
        System.out.println("收到题解删除请求");
        System.out.println("验证token");
        String token = request.getHeader("Authorization");
        Integer userId;
        try {
            DecodedJWT decodedJWT = jwtUtil.verifyToken(token);
            userId = Integer.parseInt(decodedJWT.getClaim("userId").asString());
            System.out.println("token验证成功");
        }catch (Exception e){
            System.out.println("token验证失败");
            response.setStatus(HttpStatus.UNAUTHORIZED.value());
            return null;
        }
        System.out.println("参数验证");
        if(solutionId==null){
            System.out.println("参数验证失败");
            return Result.failure();
        }
        System.out.println("参数验证通过");
        try{
            if(solutionService.deleteSolution(solutionId,userId)==0){
                System.out.println("数据库删除成功");
                return Result.success();
            }else if(solutionService.deleteSolution(solutionId,userId)==1){
                System.out.println("该题解不存在");
                return Result.failure("题解不存在");
            } else if (solutionService.deleteSolution(solutionId,userId)==2) {
                System.out.println("权限不足");
                return Result.failure("权限不足");
            } else{
                System.out.println("数据库操作发生错误");
                return Result.failure("数据库操作发生错误");
            }
        }catch (Exception e){
            System.out.println("数据库操作发生错误");
            return Result.failure("数据库操作发生错误");
        }
    }

    @GetMapping("/updateContent")
    public Result updateContent(@RequestParam("solutionId") Integer solutionId,
                                HttpServletRequest request,
                                HttpServletResponse response){
        System.out.println("收到题解删除请求");
        System.out.println("验证token");
        String token = request.getHeader("Authorization");
        Integer userId;
        try {
            DecodedJWT decodedJWT = jwtUtil.verifyToken(token);
            userId = Integer.parseInt(decodedJWT.getClaim("userId").asString());
            System.out.println("token验证成功");
        }catch (Exception e){
            System.out.println("token验证失败");
            response.setStatus(HttpStatus.UNAUTHORIZED.value());
            return null;
        }
        System.out.println("参数验证");
        if(solutionId==null){
            System.out.println("参数验证失败");
            return Result.failure();
        }
        System.out.println("参数验证通过");
        Solution solution;
        try{
            solution=solutionService.findSolutionById(solutionId);
            if(solution==null){
                return Result.failure("题解不存在");
            }
            if(!Objects.equals(solution.getUserId(), userId)){
                return Result.failure("权限不足");
            }
            return Result.success(solution);
        }catch (Exception e){
            System.out.println("查找题解失败");
            return Result.failure("查找题解失败");
        }
    }
}
